package arithmetic.leetCode.yi.ThreadSort;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-09-06
 * Time: 12:09
 */
public class ThreadWaitSort {

    private static Object myLock1 = new Object();
    private static Object myLock2 = new Object();


    public static Boolean t1Run = false;
    public static Boolean t2Run = false;

    public static void main(String[] args) {

        //----------------------------------------无限循环生产者消费者----------------------

        final Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (myLock1) {
                        t1Run = true;
                        System.out.println("产品经理规划新需求");
                        myLock1.notify();
                        try {
                            myLock1.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        });
        final Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {

                    synchronized (myLock1) {
                        if (!t1Run) {
                            System.out.println("开发人员休息一会");
                            try {
                                myLock1.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("开发人员开发需求");
                        t1Run = false;
                        myLock1.notify();
                    }


                }


            }
        });

        //----------------------------------------无限循环生产者消费者----------------------


//        final Thread thread1=new Thread(new Runnable() {
//            @Override
//            public void run() {
//                synchronized (myLock1){
//                    t1Run=true;
//                    System.out.println("产品经理规划新需求");
//                    myLock1.notify();
//                }
//            }
//        });
//
//        final Thread thread2=new Thread(new Runnable() {
//            @Override
//            public void run() {
//                synchronized (myLock1)
//                {
//                    if(!t1Run){
//                        System.out.println("开发人员休息一会");
//                        try {
//                            myLock1.wait();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//
//               synchronized (myLock2){
//                   System.out.println("开发人员开发需求");
//                   t2Run=true;
//                   myLock2.notify();
//               }
//            }
//        });
//
//
//        final Thread thread3=new Thread(new Runnable() {
//            @Override
//            public void run() {
//               synchronized (myLock2){
//                   if(!t2Run){
//                       System.out.println("测试人员休息一会");
//                       try {
//                           myLock2.wait();
//                       } catch (InterruptedException e) {
//                           e.printStackTrace();
//                       }
//                   }
//               }
//
//                 System.out.println("测试人员测试需求");
//
//            }
//        });

//        System.out.println("早上：");
//        System.out.println("测试人员来上班了...");
//        thread3.start();
//        System.out.println("产品经理来上班了...");
        thread1.start();
//        System.out.println("开发人员来上班了...");
        thread2.start();
    }
}
